// document.write("<script type='text/javascript' src='/plugin/jquery/jquery.min.js'></script>");
document.write("<script type='text/javascript' src='/plugin/SheetJs/xlsx.core.min.js'></script>");
/**
 *@JavaScriptFileName: xyh.js
 *@Description: 工具类
 *@Author: 徐一贺
 */
var xyh = {
    displayExcel: function (obj, e) {
        try {
            new Uint8Array([1, 2]).slice(0, 2);
        } catch (e) {
            console.log("[Uint8Array" + e.description + "]这里使用【Array.slice】。");
            //IE或有些浏览器不支持Uint8Array.slice()方法。改成使用Array.slice()方法
            Uint8Array.prototype.slice = Array.prototype.slice;
        }
        var url = $(obj).val();
        if (!url) {
            return;
        }
        var suffix = url.substring(url.lastIndexOf(".") + 1, url.length);
        if (!(suffix == "xls" || suffix == "xlsx")) {
            return alert("文件类型不正确");
        }
        var files = e.target.files;
        var fileReader = new FileReader();
        fileReader.readAsArrayBuffer(files[0]);// 以二进制方式打开文件
        fileReader.onload = function (ev) {
            $(obj).val('');
            var date = new Date();
            var divID = "showExcelDatePanel" + date.getSeconds();
            if ($("#" + divID) != undefined) {
                $("#" + divID).remove();
            }
            $('body').append('<div class="modal fade" id="' + divID + '" style="width: 1300px">' +
                '    <div class="modal-dialog">' +
                '        <div class="modal-content">' +
                '            <div class="modal-header">' +
                '                <button type="button" class="close" data-dismiss="modal" aria-label="Close">' +
                '                    <span aria-hidden="true">&times;</span></button>' +
                '                <h4 class="modal-title">上传Excel文件查看</h4>' +
                '            </div>' +
                '            <div class="modal-body"><table id="excelDataTable"></table></div>' +
                '            <div class="modal-footer">' +
                '                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>' +
                '            </div>' +
                '        </div>' +
                '    </div>' +
                '</div>');
            try {
                var data = ev.target.result;
                var workbook = XLSX.read(data, {type: 'array'}),
                    persons = [];
            } catch (e) {
                console.error(e);
                return alert('文件类型不正确!');
            }

            //遍历每张表读取
            for (var sheet in workbook.Sheets) {
                if (workbook.Sheets.hasOwnProperty(sheet)) {
                    persons = persons.concat(XLSX.utils.sheet_to_json(workbook.Sheets[sheet], {raw: true, header: 1}));
                    console.log(JSON.stringify(persons));
                    for (var i = 0; i < persons.length; i++) {
                        var person = persons[i];
                        var tr = '<tr id="tr-' + i + '">';
                        for (var j = 0; j < person.length; j++) {
                            tr += '<td>' + $.trim(person[j] ? person[j] : "") + '</td>';
                        }
                        for (var k = 0; k < (6 - person.length); k++) {
                            tr += '<td></td>';
                        }

                        tr += "</td>";
                        $("#excelDataTable").append(tr);
                    }
                    break;// 如果只取第一张表，就把beak注释取消
                }
            }
            $("#" + divID).modal('show');
        };
    },
    /**四舍五入*/
    rounding: function (val, v2) {
        if (isNaN(val) || val == undefined || val == null) {
            return null;
        }
        return Math.round(val * v2) / v2;
    },
    /**删除数组中的指定项*/
    arrayRemoveItem: function (arr, delVal) {
        if (arr instanceof Array) {
            var index = arr.indexOf(delVal);
            if (index > -1) {
                arr.splice(index, 1);
            }
        }
    },
    checkBrowser: function () {
        let is360 = false;
        let isIE = false;
        let isFirefox = false;
        let isChrome = false;
        let isEdge = false;
        let broName = 'Runing';
        let str = '';
        let strStart = 0;
        let strStop = 0;
        let arr = new Array();
        let temp = '';
        let userAgent = window.navigator.userAgent; //包含以下属性中所有或一部分的字符串：appCodeName,appName,appVersion,language,platform
        /*alert(userAgent);*/
        //FireFox
        if (userAgent.indexOf('Firefox') != -1) {
            isFireFox = true;
            /*broName = 'FireFox浏览器';*/
            strStart = userAgent.indexOf('Firefox');
            temp = userAgent.substring(strStart);
            broName = temp.replace('/', '版本号')

        }
        //Edge
        if (userAgent.indexOf('Edge') != -1) {
            isEdge = true;
            /*broName = 'Edge浏览器';*/
            strStart = userAgent.indexOf('Edge');
            temp = userAgent.substring(strStart);
            broName = temp.replace('/', '版本号');
        }
        //IE浏览器
        if (userAgent.indexOf('NET') != -1 && userAgent.indexOf("rv") != -1) {
            isIE = true;
            /*broName = 'IE浏览器'; */
            strStart = userAgent.indexOf('rv');
            strStop = userAgent.indexOf(')');
            temp = userAgent.substring(strStart, strStop);
            broName = temp.replace('rv', 'IE').replace(':', '版本号');
        }
        //360极速模式可以区分360安全浏览器和360极速浏览器
        if (userAgent.indexOf('WOW') != -1 && userAgent.indexOf("NET") < 0 && userAgent.indexOf("Firefox") < 0) {
            if (navigator.javaEnabled()) {
                is360 = true;
                broName = '360安全浏览器-极速模式';
            } else {
                is360 = true;
                broName = '360极速浏览器-极速模式';
            }
        }
        //360兼容
        if (userAgent.indexOf('WOW') != -1 && userAgent.indexOf("NET") != -1 && userAgent.indexOf("MSIE") != -1 && userAgent.indexOf("rv") < 0) {
            is360 = true;
            broName = '360兼容模式';
        }
        //Chrome浏览器
        if (userAgent.indexOf('WOW') < 0 && userAgent.indexOf("Edge") < 0) {
            isChrome = true;
            /*broName = 'Chrome浏览器';*/
            strStart = userAgent.indexOf('Chrome');
            strStop = userAgent.indexOf(' Safari');
            temp = userAgent.substring(strStart, strStop);
            broName = temp.replace('/', '版本号');
        }
        return isChrome;
    },
    getBrowserInfo:function (){
        let is360 = false;
        let isIE = false;
        let isFirefox = false;
        let isChrome = false;
        let isEdge = false;
        let broName = 'Runing';
        let str = '';
        let strStart = 0;
        let strStop = 0;
        let arr = new Array();
        let temp = '';
        let userAgent = window.navigator.userAgent; //包含以下属性中所有或一部分的字符串：appCodeName,appName,appVersion,language,platform
        /*alert(userAgent);*/
        //FireFox
        if (userAgent.indexOf('Firefox') != -1) {
            isFireFox = true;
            /*broName = 'FireFox浏览器';*/
            strStart = userAgent.indexOf('Firefox');
            temp = userAgent.substring(strStart);
            broName = temp.replace('/', '版本号')

        }
        //Edge
        if (userAgent.indexOf('Edge') != -1) {
            isEdge = true;
            /*broName = 'Edge浏览器';*/
            strStart = userAgent.indexOf('Edge');
            temp = userAgent.substring(strStart);
            broName = temp.replace('/', '版本号');
        }
        //IE浏览器
        if (userAgent.indexOf('NET') != -1 && userAgent.indexOf("rv") != -1) {
            isIE = true;
            /*broName = 'IE浏览器'; */
            strStart = userAgent.indexOf('rv');
            strStop = userAgent.indexOf(')');
            temp = userAgent.substring(strStart, strStop);
            broName = temp.replace('rv', 'IE').replace(':', '版本号');
        }
        //360极速模式可以区分360安全浏览器和360极速浏览器
        if (userAgent.indexOf('WOW') != -1 && userAgent.indexOf("NET") < 0 && userAgent.indexOf("Firefox") < 0) {
            if (navigator.javaEnabled()) {
                is360 = true;
                broName = '360安全浏览器-极速模式';
            } else {
                is360 = true;
                broName = '360极速浏览器-极速模式';
            }
        }
        //360兼容
        if (userAgent.indexOf('WOW') != -1 && userAgent.indexOf("NET") != -1 && userAgent.indexOf("MSIE") != -1 && userAgent.indexOf("rv") < 0) {
            is360 = true;
            broName = '360兼容模式';
        }
        //Chrome浏览器
        if (userAgent.indexOf('WOW') < 0 && userAgent.indexOf("Edge") < 0) {
            isChrome = true;
            /*broName = 'Chrome浏览器';*/
            strStart = userAgent.indexOf('Chrome');
            strStop = userAgent.indexOf(' Safari');
            temp = userAgent.substring(strStart, strStop);
            broName = temp.replace('/', '版本号');
        }
        return broName;
    }
};